package com.cskaoyan.demo;

import com.cskaoyan.utils.JDBCUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Demo1 {
    public static void main(String[] args) throws SQLException, IOException {
        // 获得连接
        Connection connection = JDBCUtils.getConnection();
        // 开启事务
        connection.setAutoCommit(false);


        try {
            // 提交预编译，防止注入
            PreparedStatement preparedStatement = connection.prepareStatement("select * from cskaoyan_user where id = ?");
            // 给占位符赋值
            preparedStatement.setInt(1,1);
            // 执行SQL语句并返回resultSet
            ResultSet resultSet = preparedStatement.executeQuery();
            // 如果有返回
            if (resultSet.next()){
                String username = resultSet.getString("username");
                System.out.println("username = " + username);
            }

            connection.commit();
        }catch (Exception e){
            e.printStackTrace();
            // 事务回滚
            connection.rollback();
        }
        // 释放资源
        JDBCUtils.release();

    }
}
